home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / OFFSCREE / CSTARTER.C1 < prev    next >
Text File  |  1991-04-30  |  8KB  |  303 lines

  1. /*****
  2.  * CStarterApp.c
  3.  *
  4.  *    Application methods for a typical application.
  5.  *
  6.  *  Copyright ⌐ 1990 Symantec Corporation.  All rights reserved.
  7.  *
  8.  *****/
  9.  
  10. #include "CStarterApp.h"
  11. #include "CStarterDoc.h"
  12.  
  13. extern    OSType    gSignature;
  14.  
  15. #define        kExtraMasters        4
  16. #define        kRainyDayFund        20480
  17. #define        kCriticalBalance    20480
  18. #define        kToolboxBalance        20480
  19.  
  20.  
  21. /***
  22.  * IStarterApp
  23.  *
  24.  *    Initialize the application. Your initialization method should
  25.  *    at least call the inherited method. If your application class
  26.  *    defines its own instance variables or global variables, this
  27.  *    is a good place to initialize them.
  28.  *
  29.  ***/
  30.  
  31. void CStarterApp::IStarterApp(void)
  32.  
  33. {
  34.     CApplication::IApplication( kExtraMasters, kRainyDayFund, 
  35.                         kCriticalBalance, kToolboxBalance);
  36.     
  37.  
  38. /*  The parameters to IApplication are the number of times to call  
  39.     MoreMasters, the total number of bytes of heap space to reserve for                       
  40.     monitoring low memory situations, and the portion of the memory
  41.     reserve to set aside for critical operations and toolbox calls.
  42.     
  43.     Four (4) is a reasonable number of MoreMasters calls,                           
  44.     but you should determine a good number for your application                     
  45.     by observing the heap using Lightsbug,                                              
  46.     TMON, or Macsbug. Set this parameter to zero, give your                         
  47.     program a rigorous work-out, then look at the heap and count                        
  48.     how many master pointer blocks have been allocated. Master                      
  49.     pointer blocks are nonrelocatable and have a size of $100                           
  50.     (hex). You should call MoreMasters at least this many                               
  51.     times -- add a few extra just to be safe. The purpose of all                        
  52.     this preflighting is to prevent heap fragmentation. You                             
  53.     don't want the Memory Manager to call MoreMasters and                           
  54.     create a nonrelocatable block in the middle of your heap. By                        
  55.     calling MoreMasters at the very beginning of the program,                           
  56.     you ensure that these blocks are allocated in a group at the                        
  57.     bottom of the heap. 
  58.                                                                         
  59.     The memory reserve is a safeguard for handling low memory                   
  60.     conditions and is used by the GrowMemory method in                              
  61.     CApplication (check there for more comments). In general,                           
  62.     your program should never request a memory block greater                        
  63.     than this reserve size without explicitly checking in                               
  64.     advance whether there is enough free memory to satisfy the                      
  65.     the request.
  66.                                                                                     
  67.  */
  68.  
  69. }
  70.  
  71.  
  72.  
  73. /***
  74.  * SetUpFileParameters
  75.  *
  76.  *    In this routine, you specify the kinds of files your
  77.  *    application opens.
  78.  *
  79.  *
  80.  ***/
  81.  
  82. void CStarterApp::SetUpFileParameters(void)
  83.  
  84. {
  85.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  86.  
  87.         /**
  88.          **    sfNumTypes is the number of file types
  89.          **    your application knows about.
  90.          **    sfFileTypes[] is an array of file types.
  91.          **    You can define up to 4 file types in
  92.          **    sfFileTypes[].
  93.          **
  94.          **/
  95.  
  96.     sfNumTypes = 1;
  97.     sfFileTypes[0] = 'TEXT';
  98.  
  99.         /**
  100.          **    Although it's not an instance variable,
  101.          **    this method is a good place to set the
  102.          **    gSignature global variable. Set this global
  103.          **    to your application's signature. You'll use it
  104.          **    to create a file (see CFile::CreateNew()).
  105.          **
  106.          **/
  107.  
  108.     gSignature = '????';
  109. }
  110.  
  111.  
  112. /***
  113.  * SetUpMenus 
  114.  *
  115.  * Set up menus which must be created at run time, such as a
  116.  * Font menu. You can eliminate this method if your application
  117.  * does not have any such menus.
  118.  *
  119. ***/
  120.  
  121.  void CStarterApp::SetUpMenus()
  122.  {
  123.  
  124.   inherited::SetUpMenus();  /*  Superclass takes care of adding     
  125.                                 menus specified in a MBAR id = 1    
  126.                                 resource    
  127.                             */                          
  128.  
  129.         /* Add your code for creating run-time menus here */    
  130.  }
  131.  
  132.  
  133.  
  134. /***
  135.  * DoCommand
  136.  *
  137.  *    Your application will probably handle its own commands.
  138.  *    Remember, the command numbers from 1-1023 are reserved.
  139.  *  The file Commands.h contains all the predefined TCL
  140.  *  commands.
  141.  *
  142.  *    Be sure to call the default method, so you can get
  143.  *    the default behvior for standard commands.
  144.  *
  145.  ***/
  146. void CStarterApp::DoCommand(long theCommand)
  147.  
  148. {
  149.     switch (theCommand) {
  150.     
  151.         /* Your commands go here */
  152.     
  153.         default:    inherited::DoCommand(theCommand);
  154.                     break;
  155.     }
  156. }
  157.  
  158.  
  159. /***
  160.  *
  161.  * UpdateMenus 
  162.  *
  163.  *   Perform menu management tasks
  164.  *
  165. ***/
  166.  
  167.  void CStarterApp::UpdateMenus()
  168.  {
  169.   inherited::UpdateMenus();     /* Enable standard commands */      
  170.  
  171.     /* Enable the commands handled by your Application class */ 
  172.  }
  173.  
  174.  
  175. /***
  176.  * Exit
  177.  *
  178.  *    Chances are you won't need this method.
  179.  *    This is the last chance your application gets to clean up
  180.  *  things like temporary files before terminating.
  181.  *
  182.  ***/
  183.  
  184. void CStarterApp::Exit()
  185.  
  186. {
  187.     /* your exit handler here */
  188. }
  189.  
  190.  
  191. /***
  192.  * CreateDocument
  193.  *
  194.  *    The user chose New from the File menu.
  195.  *    In this method, you need to create a document and send it
  196.  *    a NewFile() message.
  197.  *
  198.  ***/
  199.  
  200. void CStarterApp::CreateDocument()
  201.  
  202. {
  203.     CStarterDoc    *theDocument = NULL;
  204.     
  205.     TRY
  206.     {
  207.         theDocument = new(CStarterDoc);
  208.             
  209.             /**
  210.              **    Send your document an initialization
  211.              **    message. The first argument is the
  212.              **    supervisor (the application). The second
  213.              **    argument is TRUE if the document is printable.
  214.              **
  215.              **/
  216.         
  217.         theDocument->IStarterDoc(this, TRUE);
  218.     
  219.             /**
  220.              **    Send the document a NewFile() message.
  221.              **    The document will open a window, and
  222.              **    set up the heart of the application.
  223.              **
  224.              **/
  225.         theDocument->NewFile();
  226.     }
  227.     
  228.     CATCH
  229.     {
  230.         /*
  231.          * This exception handler gets executed if a failure occurred 
  232.          * anywhere within the scope of the TRY block above. Since 
  233.          * this indicates that a new doc could not be created, we
  234.          * check if an object had been allocated and if it has, send 
  235.          * it a Dispose message. The exception will propagate up to
  236.          * CSwitchboard's exception handler, which handles displaying
  237.          * an error alert.
  238.          */
  239.          
  240.          if (theDocument) theDocument->Dispose();
  241.  
  242.     }
  243.     ENDTRY;
  244. }
  245.  
  246. /***
  247.  * OpenDocument
  248.  *
  249.  *    The user chose Open╔ from the File menu.
  250.  *    In this method you need to create a document
  251.  *    and send it an OpenFile() message.
  252.  *
  253.  *    The macSFReply is a good SFReply record that contains
  254.  *    the name and vRefNum of the file the user chose to
  255.  *    open.
  256.  *
  257.  ***/
  258.  
  259. void CStarterApp::OpenDocument(SFReply *macSFReply)
  260.  
  261. {
  262.     CStarterDoc    *theDocument = NULL;
  263.     
  264.     TRY
  265.     {
  266.     
  267.         theDocument = new(CStarterDoc);
  268.             
  269.             /**
  270.              **    Send your document an initialization
  271.              **    message. The first argument is the
  272.              **    supervisor (the application). The second
  273.              **    argument is TRUE if the document is printable.
  274.              **
  275.              **/
  276.         
  277.         theDocument->IStarterDoc(this, TRUE);
  278.     
  279.             /**
  280.              **    Send the document an OpenFile() message.
  281.              **    The document will open a window, open
  282.              **    the file specified in the macSFReply record,
  283.              **    and display it in its window.
  284.              **
  285.              **/
  286.         theDocument->OpenFile(macSFReply);
  287.     }
  288.     
  289.     CATCH
  290.     {
  291.         /*
  292.          * This exception handler gets executed if a failure occurred 
  293.          * anywhere within the scope of the TRY block above. Since 
  294.          * this indicates that the document could not be opened, we
  295.          * send it a Dispose message. The exception will propagate up to
  296.          * CSwitchboard's exception handler, which handles displaying
  297.          * an error alert.
  298.          */
  299.          
  300.          if (theDocument) theDocument->Dispose();
  301.     }
  302.     ENDTRY;
  303. }